#include "mainwindow.h"
#include "QDebug"
#include "ui_mainwindow.h"
#include <QBitArray>
#include <QDebug>
#include <QSoundEffect>
#include <QTextCodec>
#include <QtMath>

MainWindow::MainWindow( QWidget* parent ) : QMainWindow( parent ), ui( new Ui::MainWindow ) {
    ui->setupUi( this );
    comList();
    //    onInitLHModbusLib();
    onInitLHNonStandardLib();
    //    QThread::sleep( 1000 );
    onInitLHBatter();
}

MainWindow::~MainWindow() {
    if ( lhModbusLib ) delete lhModbusLib;
    if ( nonStandard_485_drive ) delete nonStandard_485_drive;
    if ( batter ) delete batter;
    if ( thread_batter ) thread_batter->exit();
    if ( thread_non ) thread_non->exit();
    if ( thread ) thread->exit();
    delete ui;
}

void MainWindow::comList() {
    foreach ( const QSerialPortInfo& info, QSerialPortInfo::availablePorts() ) {
        QSerialPort tempSer;
        tempSer.setPort( info );
        if ( tempSer.open( QIODevice::ReadWrite ) ) {
            qDebug() << tempSer.portName();
            tempSer.close();
        }
    }
}

void MainWindow::sltModbusBack( const QString& key, const QModbusDataUnit readData, const QString& info ) {
    //    qDebug() << "key:" << key;
    //    qDebug() << "info:" << info;
    //    qDebug() << "readData:" << readData.values();
    if ( key == "car_control" ) {
        static int car_control_stat;
        if ( readData.value( 0 ) == 0 && car_control_stat != 0 ) {
            switch ( car_control_stat ) {
            case 1:
                car_control_stat = 0;
                qDebug() << "up";
                break;
            case 2:
                car_control_stat = 0;
                qDebug() << "down";
                break;
            case 4:
                car_control_stat = 0;
                qDebug() << "left";
                break;
            case 8:
                car_control_stat = 0;
                qDebug() << "right";
                break;
            case 16:
                car_control_stat = 0;
                qDebug() << "stop";
                break;
            default:
                car_control_stat = 0;
                break;
            }
        }
        else {
            car_control_stat = readData.value( 0 );
        }
    }
    if ( key == "serial_type" ) {
        //        qDebug() << "serial_type" << readData.value( 0 );
        static int seral_type_state;
        if ( readData.value( 0 ) != seral_type_state ) {
            seral_type_state = readData.value( 0 );
            QStringList list;
            switch ( readData.value( 0 ) ) {
            case 1:
                qDebug() << "切换can ";
                list.append( "切换can" );
                break;
            case 2:
                qDebug() << "切换io";
                list.append( "切换io" );
                break;
            case 3:
                qDebug() << "切换上报调度";
                list.append( "切换上报调度" );
                break;
            case 4:
                qDebug() << "切换电池";
                list.append( "切换电池" );
                break;
            case 5:
                qDebug() << "切换二维码";
                list.append( "切换二维码" );
                break;
            }
            lhModbusLib->onModbusListWriter( string_list_key, false, list );
        }
    }
}

void MainWindow::sltTransmissionError( const uchar deviceAddress ) {
    qDebug() << "sltTransmissionError->deviceAddress:" << deviceAddress;
}

void MainWindow::sltTransmissionState( const TransmissionInter::TransMissionData data ) {
    //    qDebug() << "sltTransmissionState->state:" << data.state;
    //    qDebug() << "sltTransmissionState->trans_action:" << data.trans_action;
    //    qDebug() << "sltTransmissionState->device_address:" << data.device_address;
    //    qDebug() << "sltTransmissionState->errorCode:" << data.errorCode;
    if ( data.state == TransmissionInter::TransMissionState::Start_Forward_ING ) { qDebug() << "正转"; }
    if ( data.state == TransmissionInter::TransMissionState::Start_Reverse_ING ) { qDebug() << "反转"; }
    if ( data.state == TransmissionInter::TransMissionState::Stop_ING ) { qDebug() << "停止"; }
    if ( data.state == TransmissionInter::TransMissionState::Error ) { qDebug() << "错误" << data.state; }
}

void MainWindow::sltErrorinfo( const LH_NonStandard_485_Drive::DriveErrorState driveErrorState, const QString info ) {
    qDebug() << "driveErrorState:" << driveErrorState;
    qDebug() << "info:" << info;
}

void MainWindow::on_comboBox_2_currentTextChanged( const QString& arg1 ) {
    qDebug() << "int key change " << arg1;
    int_key = arg1;
}

void MainWindow::on_comboBox_currentTextChanged( const QString& arg1 ) {
    qDebug() << "string key change " << arg1;
    string_key = arg1;
}

void MainWindow::on_send_string_clicked() {
    lhModbusLib->onModbusStringWriter( string_key, ui->string_info->toPlainText() );
}

void MainWindow::on_send_int_clicked() {
    lhModbusLib->onModbusIntWriter( int_key, ui->int_info->text().toInt() );
}

void MainWindow::on_comboBox_4_currentIndexChanged( int index ) {
    qDebug() << "adress change" << index;
    transMissionData.device_address = index + 1;
}

void MainWindow::on_comboBox_3_currentIndexChanged( int index ) {
    qDebug() << "action change" << index;
    transMissionData.trans_action = TransmissionInter::TransAction( index );
}

void MainWindow::on_pushButton_2_clicked() {
    TransmissionInter::TransMissionData data;
    data.trans_action   = transMissionData.trans_action;
    data.device_address = transMissionData.device_address;
    qDebug() << "传输 trans_action:" << data.trans_action << "device_address:" << data.device_address;
    nonStandard_485_drive->onTransmissionControl( transMissionData );
}

void MainWindow::on_pushButton_3_clicked() {

    QSoundEffect* effect = new QSoundEffect;
    effect->setSource( QUrl::fromLocalFile( "./config/sample-6s.wav" ) );
    effect->setLoopCount( 1 );  //循环次数
    effect->setVolume( 1.0f );  //音量  0~1之间
    effect->play();
}

void MainWindow::onInitLHModbusLib() {
    /********************modbus***************************/
    thread      = new QThread;
    lhModbusLib = new LHModbusLib();
    connect( lhModbusLib, &LHModbusLib::signalsModbusBack, this, &MainWindow::sltModbusBack );
    lhModbusLib->moveToThread( thread );
    connect( thread, SIGNAL( started() ), lhModbusLib, SLOT( slotLHModbusInit() ) );
    thread->start();
    thread->setPriority( QThread::TimeCriticalPriority );
    /********************modbus***************************/
}

void MainWindow::onInitLHNonStandardLib() {
    /********************nonStandard***************************/
    thread_non            = new QThread;
    nonStandard_485_drive = new LH_NonStandard_485_Drive( nullptr, "./config/motor_drive/" );
    nonStandard_485_drive->moveToThread( thread_non );
    connect( nonStandard_485_drive, &LH_NonStandard_485_Drive::sigTransmissionState, this, &MainWindow::sltTransmissionState );
    connect( nonStandard_485_drive, &LH_NonStandard_485_Drive::sigTransmissionError, this, &MainWindow::sltTransmissionError );
    connect( nonStandard_485_drive, &LH_NonStandard_485_Drive::sigErrorinfo, this, &MainWindow::sltErrorinfo );
    connect( thread_non, SIGNAL( started() ), nonStandard_485_drive, SLOT( sltOnInit() ) );
    thread_non->start();
    thread_non->setPriority( QThread::TimeCriticalPriority );
    /********************nonStandard***************************/
}

void MainWindow::onInitLHBatter() {
    thread_batter = new QThread;
    batter        = new LH_NonStandard_485_Drive( nullptr, "./config/batter/" );
    batter->moveToThread( thread_batter );
    connect( batter, &LH_NonStandard_485_Drive::sigTransmissionState, this, &MainWindow::sltTransmissionState );
    connect( batter, &LH_NonStandard_485_Drive::sigTransmissionError, this, &MainWindow::sltTransmissionError );
    connect( batter, &LH_NonStandard_485_Drive::sigErrorinfo, this, &MainWindow::sltErrorinfo );
    connect( thread_batter, SIGNAL( started() ), batter, SLOT( sltOnInit() ) );
    thread_batter->start();
    thread_batter->setPriority( QThread::TimeCriticalPriority );
}

void MainWindow::on_pushButton_5_clicked() {
    QStringList list;
    list.append( ui->string_list_info->toPlainText() );
    lhModbusLib->onModbusListWriter( string_list_key, false, list );
}

void MainWindow::on_comboBox_5_currentTextChanged( const QString& arg1 ) {
    string_list_key = arg1;
    qDebug() << "on_comboBox_5_currentTextChanged:" << string_list_key;
}

void MainWindow::on_pushButton_4_clicked() {
    QStringList list;
    list.append( "asdflajsd" );
    list.append( "sdf" );
    list.append( "sdfs" );
    list.append( "afds" );
    list.append( "asdflajsd" );
    list.append( "asdflajsd" );
    list.append( "蓝海机器人，蓝海机器人，蓝海机器人，蓝海机器人，蓝海机器人，蓝海机器人，" );
    list.append( "asdflajsd" );
    list.append( "asdflajsd" );
    lhModbusLib->onModbusListWriter( string_list_key, true, list );
}

void MainWindow::on_pushButton_6_clicked() {
    QBitArray a( 64 );
    a.setBit( 0, true );
    a.setBit( 3, true );
    a.setBit( 50, 1 );
    a.setBit( 32, true );
    uint value1 = 0;
    uint value2 = 0;
    for ( int i = 0; i < 64; i++ ) {
        if ( i < 32 ) {
            if ( a.at( i ) ) { value1 += qPow( 2, i ); }
        }
        else {
            if ( a.at( i ) ) { value2 += qPow( 2, i - 32 ); }
        }
    }
    qDebug() << "value1:" << value1;

    qDebug() << "value2:" << value2;
}
